package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.ResultData;
import com.itheima.youdianda.entity.PageBean;
import com.itheima.youdianda.entity.YddAdvimg;
import com.itheima.youdianda.entity.YddAdvpos;
import com.itheima.youdianda.exception.BusinessException;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddAdvposService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advpos")
public class YddAdvposController {
    @Autowired
    private YddAdvposService yddAdvposService;
    @Autowired
    private YddAdvimgService yddAdvimgService;

    /**
     * 分页查询
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String advposname, int page, int limit) {
        Page<YddAdvpos> advposPage = new Page<>(page, limit);
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(YddAdvpos::getAdvposname, advposname);
        yddAdvposService.page(advposPage, queryWrapper);

        //查询数量
        List<YddAdvimg> yddAdvimgs = yddAdvimgService.list();
        List<YddAdvpos> records = advposPage.getRecords();
        List<YddAdvpos> advposList = records.stream().map(yddAdvpos -> {
            int count = 0;
            for (YddAdvimg yddAdvimg : yddAdvimgs) {
                if (yddAdvimg.getAdvimgpos().equals(String.valueOf(yddAdvpos.getId()))) {
                    count++;
                }
            }
            yddAdvpos.setAdvposnum(count);
            return yddAdvpos;
        }).collect(Collectors.toList());
        advposPage.setRecords(advposList);
        //创建PageBean
        PageBean<YddAdvpos> pageBean = new PageBean<>();
        pageBean.setCount((int) advposPage.getTotal());
        pageBean.setCurrentPage((int) advposPage.getCurrent());
        pageBean.setPageSize((int) advposPage.getSize());
        pageBean.setTotalPages((int) advposPage.getPages());
        pageBean.setData(advposPage.getRecords());
        return ResultData.ok("分页查询成功", pageBean);
    }
    /***
     *广告位修改回显
     * @param yddAdvpos
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(YddAdvpos yddAdvpos) {
        YddAdvpos advpos = yddAdvposService.getById(yddAdvpos.getId());
        return ResultData.ok("数据回显成功", advpos);
    }

    /****
     * 修改广告位
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvpos yddAdvpos) {
        yddAdvposService.updateById(yddAdvpos);
        return ResultData.ok("修改成功", "");
    }

    /**
     * 添加广告位
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddAdvpos yddAdvpos){
        yddAdvposService.save(yddAdvpos);
        return ResultData.ok("添加成功",null);
    }
    @GetMapping("/del")
    public ResultData del(@RequestBody YddAdvpos yddAdvpos){
        //先判断数据是否是原有数据
        YddAdvpos advpos = yddAdvposService.getById(yddAdvpos.getId());
        if ("1".equals(advpos.getDefaultData())){
            throw new BusinessException("系统原有数据,不可以删除");
        }
        //1.获取当前要删除的广告位id
        String id = String.valueOf(yddAdvpos.getId());
        //根据id删除广告位
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddAdvimg::getAdvimgpos,yddAdvpos.getId());
        int count = yddAdvimgService.count(queryWrapper);
        if (count>0){
            throw new BusinessException("目前有广告,还不可以删除该广告位");
        }
        yddAdvposService.removeById(yddAdvpos.getId());
        return  ResultData.ok("删除成功",null);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/all")
    public ResultData all(){
        List<YddAdvpos> yddAdvpos = yddAdvposService.list();
        return ResultData.ok("",yddAdvpos);
    }
}
